<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class WeixinmenuController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("WeixinMenu");
		$this->cate = D("VarCat");
		
	}
	
	function index(){



			$result=$this->db->order(array("listorder" => "DESC","id"=>"ASC"))->select();




			import ( "Tree" );
			$tree = new \Tree ();
			$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ '
			);
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';

			$newmenus = array ();
			foreach ( $result as $m ) {
				$newmenus [$m ['id']] = $m;
			}
			foreach ( $result as $n => $r ) {

			
				$result [$n] ['parentid_node'] = ($r ['parentid']) ? ' class="child-of-node-' . $r ['parentid'] . '"' : '';

				if(UID==1){
					$result [$n] ['str_manage'] = ' <a data-toggle="tooltip" title="修改" class="btn btn-xs btn-info tooltip-info" href="' . U("edit", array("id" => $r['id'])) . '"><i class="icon-edit" ></i></a> <a data-toggle="tooltip" title="删除"  class="ajax-del btn btn-xs btn-danger	tooltip-danger"  href="' . U("delete", array("id" => $r['id']) ). '"><i class="icon-trash" ></i></a>';

				}
				else{
					$result [$n] ['str_manage'] = ' <a data-toggle="tooltip" title="修改" class="btn btn-xs btn-info tooltip-info" href="' . U("edit", array("id" => $r['id'])) . '"><i class="icon-edit" ></i></a>';

				}






			}

			$tree->init ( $result );
			$str = "<tr id='node-\$id' \$parentid_node>
					<td><input name='listorders[\$id]' type='text' size='4' value='\$listorder' class='input input-order'></td>
					<td class='hidden-480'>\$id</td>
        			<td>\$spacer\$name</td>





					<td>\$str_manage</td>
				</tr>";
			$categorys = $tree->get_tree ( 0, $str );
			$this->assign ( "list", $categorys );

			//print_r($categorys);





			$cate_name=$this->cate->where(array("id"=>$catid))->getField("name");
			$this->assign("catname",$cate_name);
			$this->display();




	}
	


	function add(){
		
		if(IS_POST){


			if($_POST["parentid"]==0){

				$top_menu=$this->db->where(array("parentid"=>0))->count();

				if($top_menu>=3){

					$this->error("一级菜单不能超过3个！");

				}

			}else{

				$sec_menu=$this->db->where(array("parentid"=>$_POST["parentid"]))->count();

				if($sec_menu>=5){

					$this->error("二级菜单不能超过5个！");

				}



			}





			
			if ($this->db->create()) {
				if ($this->db->add()!==false) {
					$this->success("添加成功！");
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{
	
			
			
			$cate=$this->db->where(array("parentid"=>0))->select();


		$this->assign("cate",$cate);
		$this->display();
		}
		}
	
	
	function edit(){
		
		
		if(IS_POST){







			
			
			if ($this->db->create()) {
				
				if ($this->db->save()!==false) {
					$this->success("保存成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}

		} else {

			$id = intval(I("get.id"));
			if (!$id) {
				$this->error("缺少参数！");
			}


			$r = $this->db->where("id=$id")->find();


			$parent_name=$this->db->where(array("id" => $r["parentid"]))->getField("name");


			$this->assign("data", $r);
			$cate = $this->db->where(array("parentid" => 0))->select();


			$this->assign("cate", $cate);
			$this->assign("parent_name", $parent_name);


			$this->display();
		}
	}
	

	
	

	function delete(){
		$id = intval(I("get.id"));

		$sec_menu=$this->db->where(array("parentid"=>$id))->count();


		if($sec_menu>0){

			$this->error("该栏目下还有子栏目,删除失败!");
		}


		if ($this->db->delete($id)) {


			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	

	
	
	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}


	public function  tongbu(){


		$data=$this->db->get_menus();

		//dump($data);

		//$data = array('button' => $data);

		//echo urldecode(json_encode($data));

		$result=$code=$this->wk->menuCreate($data);

		if(!$result["errcode"]){
			$this->success("同步成功");
		}
		else{

			$this->error("同步失败,错误码".$result["errmsg"]);
		}

//		$this->success("同步成功");


	}

	

	
	
	}
